package com.unience.aws.monitor;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;





import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;

public class SshExec {
	
	private static Log log = LogFactory.getLog(SshExec.class);
	
	 private JSch jsch;
	 private Session session;
	 private Channel channel;
	  InputStream in;
	  
	  private boolean error = false;
	
	public SshExec(){
		 jsch=new JSch();
	}
	
	public void connect(String user, String host, String key){
		
		
			 try {
				jsch.addIdentity(key);
				
				  session=jsch.getSession(user, host, 22);
				 
				 
				
				  UserInfo ui=new MyUserInfo();
			      session.setUserInfo(ui);
				  session.connect();
				  
				  
				  channel=session.openChannel("exec");
				;
				  
			} catch (JSchException e) {
				
				log.error(e);
				
				error= true;
			}
	}
	
	public String  exec(String command){
		StringBuffer ret= new StringBuffer();
		
		 try {
			((ChannelExec)channel).setCommand(command);
			 
			channel.setInputStream(null);
			
			// ((ChannelExec)channel).setErrStream(System.err);

			InputStream in=channel.getInputStream();

			  channel.connect();

			  byte[] tmp=new byte[1024];
			  while(true){
			    while(in.available()>0){
			      int i=in.read(tmp, 0, 1024);
			      if(i<0)break;
			      ret.append(new String(tmp, 0, i));
			      
			    }
			    if(channel.isClosed()){
			     log.info("exit-status: "+channel.getExitStatus());
			      break;
			    }
			    try{Thread.sleep(1000);}catch(Exception ee){}
			  }
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.error(e);
			error= true;
		} catch (JSchException e) {
			// TODO Auto-generated catch block
			log.error(e);
			error= true;
		}catch (java.lang.Exception e){
			log.error(e);
			error= true;
		}
		return ret.toString();
	}
	
	
	public void close(){
		
		if(!error){
			channel.disconnect();
		}
	      session.disconnect();
	}

	
	 public static class MyUserInfo implements UserInfo{

			
			public String getPassphrase() {
				// TODO Auto-generated method stub
				return null;
			}

			
			public String getPassword() {
				// TODO Auto-generated method stub
				return null;
			}

			
			public boolean promptPassphrase(String arg0) {
				// TODO Auto-generated method stub
				return false;
			}

			
			public boolean promptPassword(String arg0) {
				// TODO Auto-generated method stub
				return false;
			}

			
			public boolean promptYesNo(String arg0) {
				// TODO Auto-generated method stub
				return true;
			}

			
			public void showMessage(String arg0) {
				// TODO Auto-generated method stub
				
			}
			 
			 
		 }
}
